﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using System.Data;

namespace Simple.Services;

public class QHSEXyGeneralViolationService
{
    private readonly SimpleDbContext _context;
    public QHSEXyGeneralViolationService(SimpleDbContext context)
    {
        _context = context;
    }
    public async Task<List<QHSEXyGeneralViolationModel>> GetAsync()
    {
        var generalViolations = await _context.Set<QHSESysXyGeneralViolation>().AsNoTracking().ToListAsync();
        var models = MapperHelper.Map<List<QHSEXyGeneralViolationModel>>(generalViolations);
        return models;
    }
    public async Task<PageResultModel<QHSEXyGeneralViolationModel>> QueryByViolationTypeCodeAsync(GeneralViolationInputModel input)
    {
        var result = new PageResultModel<QHSEXyGeneralViolationModel>();
        var query = _context.Set<QHSESysXyGeneralViolation>().AsNoTracking().AsQueryable();

        if (!string.IsNullOrEmpty(input.ViolationTypeCode))
        {
            query = query.Where(u => EF.Functions.Like(u.ViolationTypeCode, $"%{input.ViolationTypeCode}%"));

        }
        // 获取总数量
        result.TotalRows = await query.CountAsync();

        // 分页查询
        input.PageSize = result.TotalRows;
        query = query.Page(input.PageNo, input.PageSize);
        var generalViolations = await query.ToListAsync();
        result.Rows = MapperHelper.Map<List<QHSEXyGeneralViolationModel>>(generalViolations);

        result.SetPage(input);
        result.CountTotalPage();

        return result;
    }

}
